package plugins.AI;

import com.alibaba.fastjson.JSONObject;
import kd.bos.base.AbstractBasePlugIn;
import kd.sdk.plugin.Plugin;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.ext.form.control.Markdown;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.DispatchServiceHelper;

import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;


/**
 * 基础资料插件
 */
public class HealthyAnalysis extends AbstractBasePlugIn implements Plugin {
    @Override
    public void registerListener(EventObject e) {
        // 注册点击事件，选择注册工具栏，控件也可行
        super.registerListener(e);
        this.addItemClickListeners("tbmain");
    }
    public void itemClick(ItemClickEvent e) {
        super.itemClick(e);
        if (e.getItemKey().equalsIgnoreCase("dcb4_healthy_anal")) {
            // ...处理...
            JSONObject jsonResultObject = new JSONObject();
            jsonResultObject.put("PeopleAge", this.getModel().getValue("dcb4_age").toString());
            jsonResultObject.put("PeopleHeartrate", this.getModel().getValue("dcb4_heartrate").toString());
            jsonResultObject.put("PeopleXueya", this.getModel().getValue("dcb4_xueya").toString());
            jsonResultObject.put("PeopleXuetang", this.getModel().getValue("dcb4_xuetang").toString());
            jsonResultObject.put("PeopleFayu", this.getModel().getValue("dcb4_fayu").toString());
            jsonResultObject.put("PeopleXinxueguan", this.getModel().getValue("dcb4_xinxueguan").toString());
            jsonResultObject.put("PeopleGan", this.getModel().getValue("dcb4_gan").toString());
            jsonResultObject.put("PeopleFei", this.getModel().getValue("dcb4_fei").toString());
            jsonResultObject.put("PeoplePi", this.getModel().getValue("dcb4_pi").toString());
            jsonResultObject.put("PeopleShengao", this.getModel().getValue("dcb4_shengao").toString());
            jsonResultObject.put("PeopleTizhong", this.getModel().getValue("dcb4_tizhong").toString());
            jsonResultObject.put("PeopleGangongneng", this.getModel().getValue("dcb4_gangongneng").toString());
            jsonResultObject.put("c", this.getModel().getValue("dcb4_xinfei").toString());

            // 调用GPT开发平台微服务
            Map<String, String> variableMap = new HashMap<>();
            variableMap.put("healthInfo", jsonResultObject.toJSONString());

            Object[] params = new Object[]{
                    //GPT提示编码
                    getPromptFid("prompt-2507180D058576"),
                    "开始分析健康状况",
                    variableMap
            };

            Map<String, Object> result = DispatchServiceHelper.invokeBizService(
                    "ai", "gai", "GaiPromptService", "syncCall", params
            ); // 固定写法

            JSONObject jsonObjectResult = new JSONObject(result); // 固定写法
            JSONObject jsonObjectData = jsonObjectResult.getJSONObject("data"); // 固定写法
            this.getModel().setValue("dcb4_healthtxt", jsonObjectData.getString("llmValue")); // llmValue是固定的写法

            Markdown mk = this.getView().getControl("dcb4_healthmarkdown");
            mk.setText(jsonObjectData.getString("llmValue"));
        }
    }

    // 获取GPT提示的Fid（固定函数）
    public long getPromptFid(String billNo) {
        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(
                "gai_prompt",
                "number,id",
                new QFilter[]{new QFilter("number", QCP.equals, billNo)}
        );
        return (dynamicObject).getLong("id");
    }

    @Override
    public void afterBindData(EventObject eventObject) {
        Markdown mk = this.getView().getControl("dcb4_healthmarkdown");
        Object txtValue = this.getModel().getValue("dcb4_healthtxt");
        if (txtValue != null) {
            mk.setText(txtValue.toString());
        }
    }
}

